package com.pingfangushi.kaptcha.spring.boot;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * 验证码自动配置类
 *
 * @author SanLi
 * Created by 2689170096@qq.com on 2018/10/21 12:21
 */
@Configuration
@ConditionalOnClass(DefaultKaptcha.class)
@EnableConfigurationProperties(KaptchaPorperties.class)
public class KaptchaAutoConfiguration {

    /**
     * 注入 KaptchaPorperties
     */
    private final KaptchaPorperties kaptchaPorperties;

    /**
     * @param kaptcha {@link KaptchaPorperties}
     */
    public KaptchaAutoConfiguration(KaptchaPorperties kaptcha) {
        this.kaptchaPorperties = kaptcha;
    }

    /**
     * @return {@link DefaultKaptcha}
     */
    @Bean
    @ConditionalOnMissingBean
    public DefaultKaptcha defaultKaptcha() {
        Properties prop = new Properties();
        //宽度
        prop.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, String.valueOf(kaptchaPorperties.getWidth()));
        //高度
        prop.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, String.valueOf(kaptchaPorperties.getHeight()));
        /**
         * 内容配置
         * @author SanLi
         * Created by 2689170096@qq.com on 2018/10/21 14:16
         */
        KaptchaPorperties.Content content = kaptchaPorperties.getContent();
        //内容源
        prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_STRING, content.getSource());
        //内容长度
        prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, String.valueOf(content.getLength()));
        //内容间隔
        prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_SPACE, String.valueOf(content.getSpace()));
        /**背景色
         * @author SanLi
         * Created by 2689170096@qq.com on 2018/10/21 14:18
         */
        KaptchaPorperties.BackgroundColor backgroundColor = kaptchaPorperties.getBackgroundColor();
        prop.setProperty(Constants.KAPTCHA_BACKGROUND_CLR_FROM, backgroundColor.getFrom());
        prop.setProperty(Constants.KAPTCHA_BACKGROUND_CLR_TO, backgroundColor.getTo());
        /** 边框
         * @author SanLi
         * Created by 2689170096@qq.com on 2018/10/21 14:18
         */
        KaptchaPorperties.Border border = kaptchaPorperties.getBorder();
        //是否开启边框
        prop.setProperty(Constants.KAPTCHA_BORDER, border.getEnabled() ? "yes" : "no");
        //边框颜色
        prop.setProperty(Constants.KAPTCHA_BORDER_COLOR, border.getColor());
        //边框厚度
        prop.setProperty(Constants.KAPTCHA_BORDER_THICKNESS, String.valueOf(border.getThickness()));
        /** 字体
         * @author SanLi
         * Created by 2689170096@qq.com on 2018/10/21 14:18
         */
        KaptchaPorperties.Font font = kaptchaPorperties.getFont();
        //字体名称
        prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, font.getName());
        //字体大小
        prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, String.valueOf(font.getSize()));
        //字体颜色
        prop.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, font.getColor());
        //绘制实现类
        prop.setProperty(Constants.KAPTCHA_OBSCURIFICATOR_IMPL, font.getTextProducerImpl());

        /** 配置
         * @author SanLi
         * Created by 2689170096@qq.com on 2018/10/21 14:22
         */
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(new Config(prop));
        return defaultKaptcha;
    }

    /**
     * 注入GoogleKaptcha
     *
     * @author SanLi
     * Created by 2689170096@qq.com on 2018/10/21 14:58
     */
    @Bean
    @ConditionalOnMissingBean
    public GoogleKaptcha googleKaptcha(DefaultKaptcha defaultKaptcha) {
        return new GoogleKaptcha(defaultKaptcha, kaptchaPorperties);
    }
}
